<template>
  <div>
    <el-tree :data="menus" :props="defaultProps" :expand-on-click-node="false"
             show-checkbox node-key="catId"
             :default-expanded-keys="expandedKey">
    <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <el-button
            type="text"
            size="mini"
            @click="() => append(data)"
            v-if="node.level !== 3">
            新增
          </el-button>
          <el-button
            type="text"
            size="mini"
            @click="() => edit(data)">
            修改
          </el-button>
          <el-button
            type="text"
            size="mini"
            @click="() => remove(node, data)"
            v-if="node.childNodes.length === 0">
            删除
          </el-button>
        </span>
      </span>
    </el-tree>
    <el-dialog
      :title="fromTitle"
      :visible.sync="dialogVisible"
      width="30%">
      <el-form :model="category">
        <el-form-item label="分类名称">
          <el-input v-model="category.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="单位">
          <el-input v-model="category.productUnit" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="submitData">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>


<script>
export default {
  props: {},
  data() {
    return {
      fromType: "",
      fromTitle: "",
      category: {
        catId: "",
        name: "",
        parentCid: "",
        catLevel: "",
        showStatus: "",
        sort: "",
        productUnit: ""
      },
      dialogVisible: false,
      expandedKey: [],
      menus: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created() {
    this.getMenus()
  },
  // activated() {
  //   this.getMenus()
  // },
  methods: {
    getMenus(data) {
      this.$http({
        url: this.$http.adornUrl('/product/category/list/tree'),
        method: 'get'
      }).then(({data}) => {
        console.log(data.data)
        this.menus = data.data
      })
    },
    edit(data) {
      this.fromType = 'edit'
      this.fromTitle = '修改'
      this.dialogVisible = true
      console.log(data)
      this.category.name = data.name
      this.category.catId = data.catId
      this.category.catLevel = parseInt(data.catLevel)
      this.category.productUnit = data.productUnit
    },
    append(node) {
      this.fromType = 'add'
      this.fromTitle = '新增'
      this.dialogVisible = true
      this.category.name = ''
      this.category.catId = null
      this.category.parentCid = node.catId
      this.category.catLevel = parseInt(node.catLevel) + 1
      this.category.showStatus = 1
      this.category.sort = 0
    },
    submitData(data){
      if (this.fromType === 'add'){
        this.addCategory()
      }else{
        this.editCategory()
      }
    },
    editCategory(){
      this.$http({
        url: this.$http.adornUrl('/product/category/update'),
        method: 'post',
        data: this.$http.adornData(this.category, false)
      }).then(({data}) => {
        if (data.code === 0) {
          this.$message({
            message: '修改成功',
            type: 'success'
          });
          this.getMenus()
        } else {
          this.$message.error('修改失败');
        }
      })
      this.dialogVisible = false
    },
    addCategory() {
      this.$http({
        url: this.$http.adornUrl('/product/category/save'),
        method: 'post',
        data: this.$http.adornData(this.category, false)
      }).then(({data}) => {
        if (data.code === 0) {
          this.$message({
            message: '添加成功',
            type: 'success'
          });
          this.getMenus()
        } else {
          this.$message.error('删除失败');
        }
      })
      this.dialogVisible = false
    },

    remove(node, data) {
      var ids = [data.catId]
      console.log(ids.toString())
      console.log(node, data)
      this.$confirm('是否删除【' + data.name + '】菜单', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/product/category/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({data}) => {
          if (data.code === 0) {
            this.$message({
              message: '删除成功',
              type: 'success'
            });
            this.getMenus()
            this.expandedKey = [node.data.parentCid]
          } else {
            this.$message.error('删除失败');
          }

        })
      }).catch(() => {
        this.$message({
          type: 'warning',
          message: '已取消删除'
        });
      });
    },
  }
}
</script>
